home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / CIncludesTool / modified version / source files / dictionaries.c < prev    next >
Encoding:
Text File  |  1993-09-09  |  6.5 KB  |  359 lines  |  [TEXT/MPS ]

  1. // dictionaries.c
  2. //
  3. // distionary functions for CIncludesCode MPW tool
  4.  
  5.  
  6. #include <StdLib.h>
  7. #include <Memory.h>
  8. #include <StdIO.h>
  9. #include <String.h>
  10. #include "CIncludesCode.h"
  11.  
  12. extern long totalWords;
  13. extern long numFiles;
  14. extern ptrArray *argvPtr;
  15. extern strArray **filesHdl;
  16. extern Handle dictionary[numDictionaries];
  17. extern Handle dependencies;
  18.  
  19. short dictionaryIndex(char ch)
  20. {
  21. if (ch == '_')
  22.     {
  23.     return 0;
  24.     }  // if ch
  25. else
  26.     {
  27.     return ((ch & 0xDF) - '@');
  28.     }  // else if ch
  29. }  // dictionaryIndex()
  30.  
  31. void insertWord(short fileIndex,long filePosition,char *s)
  32. {
  33. Handle dictHdl;
  34. long curSize;
  35. long endOffset;
  36. char *p;
  37. short len = strlen(s);
  38.     
  39. if (len == 0)
  40.     {
  41.     return;
  42.     }  // if len
  43.  
  44. totalWords++;
  45.     
  46. dictHdl = dictionary[dictionaryIndex(*s)];
  47. curSize = GetHandleSize(dictHdl);
  48. endOffset = *((long*) (*dictHdl));
  49.     
  50. if ((8 + endOffset + len + 1) > curSize)
  51.     {
  52.     SetHandleSize(dictHdl,curSize + 1024);
  53.     if (MemError())
  54.         {
  55.         fprintf(stderr,"MemError = %d\n",MemError());
  56.         errorExit ("Could not resize dictionary handle!");
  57.         }  // if MemError()
  58.     }  // if 8
  59.  
  60. p = *dictHdl + endOffset;
  61. putShort(p,fileIndex);
  62. putLong((p + 2),filePosition);
  63. strcpy((p + (2 + 4)),s);
  64.     
  65. *((long*) (*dictHdl)) += ((2 + 4 + 1) + len);    // fileIndex = 2,filePostion = 4,'\0' = 1
  66. }  // insertWord()
  67.  
  68. #define W(s)    insertWord(-1,0,s)
  69.  
  70. void fillReservedWords(void)
  71. {
  72. W("asm");
  73. W("auto");
  74. W("break");
  75. W("case");
  76. W("catch");
  77. W("char");
  78. W("class");
  79. W("comp");
  80. W("const");
  81. W("continue");
  82. W("default");
  83. W("delete");
  84. W("do");
  85. W("double");
  86. W("else");
  87. W("entry");
  88. W("enum");
  89. W("extended");
  90. W("extern");
  91. W("float");
  92. W("for");
  93. W("friend");
  94. W("goto");
  95. W("if");
  96. W("inherited");
  97. W("inline");
  98. W("int");
  99. W("long");
  100. W("new");
  101. W("operator");
  102. W("overload");
  103. W("pascal");
  104. W("private");
  105. W("protected");
  106. W("public");
  107. W("register");
  108. W("return");
  109. W("short");
  110. W("signed");
  111. W("sizeof");
  112. W("static");
  113. W("struct");
  114. W("switch");
  115. W("template");
  116. W("this");
  117. W("typedef");
  118. W("union");
  119. W("unsigned");
  120. W("virtual");
  121. W("void");
  122. W("volatile");
  123. W("while");
  124. W("cdecl");
  125. W("near");
  126. W("far");
  127. W("huge");
  128. W("interrupt");
  129. }  // fillReservedWords
  130.  
  131. void initDictionaries(void)
  132. {
  133. short i;
  134.     
  135. fprintf(stderr,"Initializing dictionaries...\n");    
  136.  
  137. for (i = 0; i < numDictionaries; ++i)
  138.     {
  139.     dictionary[i] = NewHandle(4);
  140.     *((long*) (*dictionary[i])) = 4;   /* "End Pointer" */
  141.     }  // for i
  142.  
  143. fillReservedWords();
  144. }  // initDictionaries()
  145.  
  146.  
  147. void DisposDictionaries(void)
  148. {
  149. short i;
  150.     
  151. for (i = 0; i < numDictionaries; ++i)
  152.     {
  153.     DisposHandle(dictionary[i]);
  154.     }  // for i
  155. }  // DisposeDictionaries()
  156.  
  157. char *extractIdentifier(char *dest,char *src)
  158. {
  159. char *destPtr = dest;
  160. short count;
  161.     
  162. for (count = 0; validChar(*src) && (count < 64); ++count)
  163.     {
  164.     *dest++ = *src++;
  165.     }  // for count
  166. if (*src == '.')       // return NULL string for structure members,etc.
  167.     {
  168.     while ((*src == '.') || validChar(*src))
  169.         {
  170.         src++;
  171.         }  // while *src
  172.     *destPtr = '\0';
  173.     }  // if *src
  174. else
  175.     {
  176.     *dest = '\0';
  177.     }  // else if *src
  178. return src;
  179. }  // extractIdentifier
  180.  
  181. long nextIdentifier(char *s,Handle dataHdl,long offset,long totalSize)
  182. // parse text looking for valid identifiers
  183.  
  184. {
  185. char *base = StripAddress(*dataHdl);
  186. char *p = base + offset;
  187. char *q = base + totalSize;
  188. char ch;
  189. Boolean done;
  190.  
  191. while (p < q)
  192.     {
  193.     ch = *p++;
  194.  
  195.     if (ch == '"')
  196.         {
  197.         // skip string literals
  198. //        while ((*p++ != '"') || (*(p-2) == '\\'))    // original code does not correctly handle cases like "hello\\"
  199. //            ;
  200.         do
  201.             {
  202.             ch = *p++;
  203.             done = ((ch == '"') && !((*(p - 1) == '\\') && (*(p - 2) != '\\')));
  204.             }  // do
  205.         while(!done);
  206.         }  // if ch
  207.     else
  208.         {
  209.         if (ch == '#')
  210.             {
  211.             // skip pre-processor commands
  212.             while (validChar(*p++))
  213.                 ;
  214.             }  // if ch
  215.         else
  216.             {
  217.             if ((ch == '/') && (*p == '/'))
  218.                 {
  219.                 // skip comments
  220.                 while (*p++ != '\n')
  221.                     ;
  222.                 }  // if ch
  223.             else
  224.                 {
  225.                 if ((ch == '/') && (*p == '*'))
  226.                     {
  227.                     // skip comments
  228.                     while ((*p++ != '/') || (*(p-2) != '*'))
  229.                         ;
  230.                     }  // if ch
  231.                 else
  232.                     {
  233.                     if (validStart(ch))
  234.                         {
  235.                         p = extractIdentifier(s,p - 1);
  236.                         if (*s)
  237.                             {
  238.                             return ((long) (p - base));
  239.                             }  // if *s
  240.                         }  // if validStart()
  241.                     }  // else if ch
  242.                 }  // else if ch
  243.             }  // else if ch
  244.         }  // else if ch
  245.     }  // while p
  246. return totalSize;
  247. }  // nextIdentifier()
  248.  
  249. long searchDict(char *s,Handle dictHdl)
  250. // returns offset or 0
  251.  
  252. {
  253. char *base = StripAddress(*dictHdl);
  254. char *p = base + (4 + 2 + 4);                        // endOffset = 4,fileIndex = 2,filePostion = 4; 
  255. char *limit    = base + *((long*) base);
  256.  
  257. if (*s)
  258.     {
  259.     while (p < limit)
  260.         {
  261.         if (strcmp(p,s) == 0)
  262.             {
  263.             return ((long) (p - base - (2 + 4)));    // fileIndex = 2,filePostion = 4
  264.             }  // if strcmp()
  265.         else
  266.             {
  267.             p += strlen(p) + (2 + 4 + 1);            // fileIndex = 2,filePostion = 4,'\0' = 1
  268.             }  // else if strcmp()
  269.         }  // while p
  270.     }  // if s
  271. return 0;
  272. }  // searchDict()
  273.  
  274. long parseFile(Handle dataHdl,short fileIndex)
  275. {
  276. short oldFile;
  277. long offset;
  278. long count = 0;
  279. long pos   = 0;
  280. long totalSize = GetHandleSize(dataHdl) - 1;
  281. Handle dictHdl;
  282. char s[64];
  283.  
  284. while ((pos = nextIdentifier(s,dataHdl,pos,totalSize)) < totalSize)
  285.     {
  286.     dictHdl = dictionary[dictionaryIndex(*s)];
  287.  
  288.     offset = searchDict(s,dictHdl);
  289.     if (offset)
  290.         {
  291.         oldFile = getShort(*dictHdl + offset);
  292.             
  293.         if ((oldFile != -1) && (oldFile != fileIndex) && isDependent(*dependencies,oldFile,fileIndex))
  294.             {
  295.             putShort((*dictHdl + offset),fileIndex);
  296.             putLong((*dictHdl + offset + 2),pos);
  297.             }  // if oldFile
  298.         }  // if offset
  299.     else
  300.         {
  301.         count++;
  302.         insertWord(fileIndex,pos,s);
  303.         }  // else if offset
  304.     }  // while pos
  305. return count;
  306. }  // parseFile()
  307.  
  308. void fillDictionaries(void)
  309. {
  310. short i;
  311. short maxLength = maxFilename();
  312. long count = 0;
  313. Handle dataHdl;
  314.     
  315. fprintf(stderr,"\nParsing Files:\n");
  316. for (i = 0; i < numFiles; ++i)
  317.     {
  318.     fprintf(stderr,"    %-*s",maxLength,(**filesHdl)[i]);
  319.     dataHdl = loadDataFile((*argvPtr)[i + 2]);
  320.     count = parseFile(dataHdl,i);
  321.     DisposHandle(dataHdl);
  322.     fprintf(stderr,"  %4d new\n",count);
  323.     }  // for i
  324.     
  325. fprintf(stderr,"\nTotal Entries = %ld\n",totalWords);
  326. }  // fillDictionaries()
  327.  
  328. void writeDictionary(Handle dictHdl)
  329. {
  330. long pos = 4;
  331. long endOffset = *((long*) (*dictHdl));
  332. char s[64];
  333.     
  334. while (pos < endOffset)
  335.     {
  336.     fprintf(stderr,"%3d  ",getShort(*dictHdl + pos));
  337.     pos += 2;
  338.     strcpy(s,*dictHdl + pos);
  339.     fprintf(stderr,"%s\n",s);
  340.     pos += strlen(s) + 1;
  341.     }  // while pos
  342. }  // writeDictionary()
  343.  
  344. void writeAllDictionaries(void)
  345. {
  346. short i;
  347.     
  348. for (i = 0; i < numDictionaries; ++i)
  349.     {
  350.     writeDictionary(dictionary[i]);
  351.     }  // for i
  352. }  // writeAllDictionaries()
  353.  
  354. void writeSpecificDirectory(char ch)
  355. {
  356. writeDictionary(dictionary[dictionaryIndex(ch)]);
  357. }  // writeSpecificDirectory()
  358.  
  359. // end of dictionaries.c